//
//  UIView+Tips.m
//  FullShareTop
//
//  Created by lqq on 17/3/21.
//  Copyright © 2017年 FSB. All rights reserved.
//

#import "UIView+Tips.h"
#import "ShowTipsView.h"
#import "UIView+Extension.h"
#import "NSError+Extension.h"
@implementation UIView (Tips)

- (void)showLoading
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ShowTipsView class]) owner:nil options:nil] firstObject];
    showView.loadingView.hidden = NO;
    showView.descLabel.hidden = YES;
    showView.callBackBtn.hidden = YES;
    [self showView:showView];
}


- (void)showError:(NSError *)error refreshBlock:(void(^)(void))refreshBlock
{
    if (error.code == NetErrorCode) {
        [self showErrorStr:@"网络断开连接" refreshBlock:refreshBlock];
    } else {
        [self showErrorStr:error.errorMsg refreshBlock:refreshBlock];
    }
}

- (void)showErrorStr:(NSString *)errorStr refreshBlock:(void(^)(void))refreshBlock
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowTipsView"owner:nil options:nil] lastObject] ;
    showView.loadingView.hidden = YES;
    showView.descLabel.hidden = NO;
    showView.callBackBtn.hidden = NO;
    showView.callBackBlock = refreshBlock;
    showView.descLabel.text = errorStr;
    [showView.callBackBtn setTitle:@"点击重试" forState:UIControlStateNormal];
    [self showView:showView];
    
}





- (void)showEmptyingView
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowTipsView"owner:nil options:nil] lastObject];
    showView.loadingView.hidden = YES;
    showView.descLabel.hidden = NO;
    showView.descLabel.text = @"空空如也";
    showView.callBackBtn.hidden = YES;
    [self showView:showView];
}

/**
 展示带有图片和描述的空页面
 */
- (void)showEmptyViewWithPic:(UIImage *)picture desc:(NSString *)desc
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowEmptyPhotoView"owner:nil options:nil] lastObject];
    showView.pictureImageVIew.image = picture;
    showView.pictureDescLabel.text = desc;
    [self showView:showView];
}

/**
 空数据-暂无训练记录
 */
- (void)showEmptyingRecordViewCallBackBlock:(void(^)(void))callBackBlock;
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowEmptyRecordView"owner:nil options:nil] lastObject];
    showView.callBackBlock = callBackBlock;
    [self showView:showView];
}

/**
 空数据-无成员
 */
- (void)showEmptyingGroupViewCallBackBlock:(void(^)(void))callBackBlock
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowEmptyGroupView"owner:nil options:nil] lastObject];
    showView.callBackBlock = callBackBlock;
    [self showView:showView];
}

/**
 自定义空数据内容
 */
- (void)showEmptyWithDesc:(NSString *)desc callBackTitle:(NSString *)callBackTitle callBack:(void(^)(void))callBackBlock
{
    [self hideStatusView];
    ShowTipsView *showView = [[[NSBundle mainBundle] loadNibNamed:@"ShowTipsView"owner:nil options:nil] lastObject];
    showView.loadingView.hidden = YES;
    showView.descLabel.hidden = NO;
    showView.descLabel.text = desc;
    if (callBackBlock && callBackTitle) {
        showView.callBackBtn.hidden = NO;
        [showView.callBackBtn setTitle:callBackTitle forState:UIControlStateNormal];
    } else {
        showView.callBackBtn.hidden = YES;
    }
    
    showView.callBackBlock = callBackBlock;
    [self showView:showView];
}


- (void)hideStatusView
{
    UIView *existView = [self viewWithTag:-10000];
    if (existView) {
        [existView removeFromSuperview];
    }
}


- (void)showView:(UIView *)showView
{
    if (showView) {
        showView.backgroundColor = self.backgroundColor;
        showView.tag = -10000;

        if ([self isKindOfClass:[UIScrollView class]]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setNeedsLayout];
                UIScrollView *scrollView = (UIScrollView *)self;
                CGFloat top = scrollView.insetTop;
                CGFloat bottom = scrollView.insetBottom;
                showView.frame = self.bounds;
                showView.top += top;
                showView.height -= top;
                showView.bottom += bottom;
                showView.height -= bottom;
                [self addSubview:showView];
                [self bringSubviewToFront:showView];
                
                
            });
  
        } else {
            [self addSubview:showView];
            [self bringSubviewToFront:showView];
            [showView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(self);
            }];
        }
        
    }
}


@end
